home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / astrolog / src / xtras.c < prev   
C/C++ Source or Header  |  1995-08-11  |  7KB  |  284 lines

  1. /*                                                               -*- C -*-
  2. **  Extras for ASTROLOG
  3. **
  4. **  (c)Copyright 1995 by Tobias Ferber,  All Rights Reserved
  5. */
  6.  
  7. /* $VER: $Id: xtras.c,v 1.4 1995/06/26 16:13:29 tf Exp $ */
  8.  
  9. #include "astrolog.h"
  10.  
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13.  
  14. static struct { char *s; int c; } ctab[]=
  15. {
  16.   { "{black}",     kBlack   },
  17.   { "{maroon}",    kMaroon  },
  18.   { "{dkgreen}",   kDkGreen },
  19.   { "{orange}",    kOrange  },
  20.   { "{dkblue}",    kDkBlue  },
  21.   { "{purple}",    kPurple  },
  22.   { "{dkcyan}",    kDkCyan  },
  23.   { "{ltgray}",    kLtGray  },
  24.   { "{dkgray}",    kDkGray  },
  25.   { "{red}",       kRed     },
  26.   { "{green}",     kGreen   },
  27.   { "{yellow}",    kYellow  },
  28.   { "{blue}",      kBlue    },
  29.   { "{magenta}",   kMagenta },
  30.   { "{cyan}",      kCyan    },
  31.   { "{white}",     kWhite   },
  32.   { (char *)0,   0        }
  33. };
  34.  
  35. static int current_line   = 1;
  36. static int current_column = 1;
  37.  
  38. #define XBUF 1024  /* length of line buffers */
  39.  
  40. /*
  41. **  This function is similar to DrawPrint() [see "xcharts0.c"] and
  42. **  prints given string `s' to the screen
  43. */
  44.  
  45. /****** xtras/xPutString *****************************************************
  46. *
  47. *   NAME
  48. *       xPutString -- Render text on the graphic screen
  49. *
  50. *   SYNOPSIS
  51. *       xPutString(str);
  52. *
  53. *       void xPutString(char *);
  54. *
  55. *   FUNCTION
  56. *       This procedure prints given string `str' onto astrolog's graphic chart
  57. *       display at the position where the previous call's output ended.  I.e.:
  58. *       xPutString() has an invisible cursor which is advanced to the end of
  59. *       it's output, each time you call this function.  The usual "escaped"
  60. *       character sequences (like '\n' ot '\b') are handled by this function
  61. *       as well.
  62. *
  63. *       The text is rendered using the current pen as set via DrawColor().
  64. *       However, some special macros can be used to change the color within
  65. *       a string.  These macros are:
  66. *
  67. *            {black}     kBlack               {dkgray}    kDkGray
  68. *            {maroon}    kMaroon              {red}       kRed
  69. *            {dkgreen}   kDkGreen             {green}     kGreen
  70. *            {orange}    kOrange              {yellow}    kYellow
  71. *            {dkblue}    kDkBlue              {blue}      kBlue
  72. *            {purple}    kPurple              {magenta}   kMagenta
  73. *            {dkcyan}    kDkCyan              {cyan}      kCyan
  74. *            {ltgray}    kLtGray              {white}     kWhite
  75. *
  76. *   INPUTS
  77. *       str   - The string to render
  78. *
  79. *   RESULT
  80. *       none
  81. *
  82. *   EXAMPLE
  83. *       xPrintAt(3,5,"");   \* initialize cursor position *\
  84. *       xPutSting("This text is rendered using the current color,\n"
  85. *                 "{white}whereas {red}this {green}changes {purple}it");
  86. *
  87. *   NOTES
  88. *       xPutString() restores the initial pen if a color macro changes it.
  89. *       So xPutString("{red}") is not the same as DrawColor(kRed).
  90. *
  91. *   BUGS
  92. *       '\t' is not implemented.
  93. *
  94. *   SEE ALSO
  95. *       xPrint(), xPrintAt()
  96. *
  97. ******************************************************************************
  98. *
  99. *
  100. */
  101.  
  102. void xPutString(s)
  103. const char *s;
  104. {
  105.   char buf[XBUF];
  106.   int i,n, pen= gi.kiCur;
  107.  
  108. #define showit do { buf[n]= '\0'; \
  109.   DrawSz( buf, current_column * xFont * gi.nScaleT, \
  110.                current_line   * yFont * gi.nScaleT, dtLeft | dtBottom ); } while(0)
  111.  
  112.   for(n=0; *s; s++)
  113.   {
  114.     switch(*s)
  115.     {
  116.       case '\n':
  117.         if(n>0) { showit; n=0; }
  118.         current_line++;
  119.         current_column= 1;
  120.         break;
  121.  
  122.       case '\b':
  123.         if(n>0) { showit; current_column+=n; n=0; }
  124.         if(current_column > 1)
  125.           current_column--;
  126.         break;
  127.  
  128.       case '\r':
  129.         if(n>0) { showit; n=0; }
  130.         current_line++;
  131.         break;
  132.  
  133.       case '{':
  134.         {
  135.           char *a, *b;
  136.  
  137.           for(i=0; ctab[i].s; i++)
  138.           {
  139.             a= s; b= ctab[i].s;
  140.             while(*a && *b && *a==*b) { ++a; ++b; }
  141.  
  142.             if(*b=='\0')
  143.               break;
  144.           }
  145.  
  146.           if(b && *b=='\0') /* we matched a color */
  147.           {
  148.             if(n>0) { showit; current_column+=n; n=0; }
  149.             DrawColor(ctab[i].c);
  150.             s= --a;
  151.             break;
  152.           }
  153.           else buf[n++]= *s; /* ordinary brace */
  154.         }
  155.         break;
  156.  
  157.       default:
  158.         buf[n++]= *s;
  159.         break;
  160.     }
  161.   }
  162.  
  163.   if(n>0) { showit; current_column+=n; n=0; }
  164.  
  165. #undef showit
  166.  
  167.   gi.kiCur= pen;
  168. }
  169.  
  170. /****** xtras/xPrint *********************************************************
  171. *
  172. *   NAME
  173. *       xPrint -- Render a format string to astrolog's graphic screen
  174. *
  175. *   SYNOPSIS
  176. *       xPrint(fmt,...);
  177. *
  178. *       void xPrint(char *,...);
  179. *
  180. *   FUNCTION
  181. *       This procedure does basically the same as xPutString(), i.e. it 
  182. *       renders a string to the graphic screen, using astrolog's internal
  183. *       font.  The difference between xPutString() and xPrint() is that
  184. *       xPrint() performs printf() like substitutions in the given
  185. *       format string `fmt'.
  186. *
  187. *   INPUTS
  188. *       fmt   - A printf() like format string and
  189. *       ...     it's arguments
  190. *
  191. *   RESULT
  192. *       none
  193. *
  194. *   EXAMPLE
  195. *
  196. *   NOTES
  197. *
  198. *   BUGS
  199. *
  200. *   SEE ALSO
  201. *       xPutString(), xPrintAt()
  202. *
  203. ******************************************************************************
  204. *
  205. *
  206. */
  207.  
  208. void xPrint(fmt, ...)
  209. const char *fmt;
  210. {
  211.   char *buf[XBUF];
  212.  
  213.   va_list argp;
  214.   va_start(argp,fmt);
  215.   vsprintf(buf,(char *)fmt,argp);
  216.  
  217.   xPutString(buf);
  218.   va_end(argp);
  219. }
  220.  
  221.  
  222. /****** xtras/xPrintAt *******************************************************
  223. *
  224. *   NAME
  225. *       xPrintAt -- Render format string at given position on the screen
  226. *
  227. *   SYNOPSIS
  228. *       xPrintAt(line,column, fmt,...);
  229. *
  230. *       void xPrintAt(int,int, char *,...);
  231. *
  232. *   FUNCTION
  233. *       This procedure moves an invisible cursor to given location
  234. *       (line,column) on astrolog's screen and renders the format string
  235. *       `fmt' at this location via xPutString() after expanding it using
  236. *       the given arguments `...' (if present).
  237. *
  238. *   INPUTS
  239. *       line,column   - The position where the first character shall be 
  240. *                       rendered on the screen
  241. *       fmt           - A printf() like format string and
  242. *       ...             it's arguments
  243. *
  244. *   RESULT
  245. *       none
  246. *
  247. *   EXAMPLE
  248. *       xPrintAt(5,3,"{green}I compute {white}1+1={red}%d{green}.",1+1);
  249. *
  250. *   NOTES
  251. *       You should *always* use xPrintAt() before using either xPrint()
  252. *       or xPutString() in order to initialize the cursor position.
  253. *
  254. *   BUGS
  255. *
  256. *   SEE ALSO
  257. *       xPutString(), xPrint()
  258. *
  259. ******************************************************************************
  260. *
  261. *
  262. */
  263.  
  264. void xPrintAt(line,column, fmt, ...)
  265. int line,column;
  266. const char *fmt;
  267. {
  268.   char *buf[XBUF];
  269.  
  270.   va_list argp;
  271.   va_start(argp,fmt);
  272.  
  273.   vsprintf(buf,(char *)fmt,argp);
  274.  
  275.   if(line > 0)    current_line   = line;
  276.   else            current_line   = 1;
  277.  
  278.   if(column > 0)  current_column = column;
  279.   else            current_column = 1;
  280.  
  281.   xPutString(buf);
  282.   va_end(argp);
  283. }
  284.